<?php

/**
 * This class is used as the base view for any views.
 * It helps define the standard js/html/metatag functions
 */
class folio_BaseView {

    protected $html = '';
    protected $js = '';
    protected $metatags = array();

    final public function html() {
        return $this->html;
    }
    final public function js() {
        return $this->js;
    }
    final public function metatags() {
        if( is_array( $this->metatags))
            return $this->metatags;
        else
            return array();
    }

    /**
     * Use to add a control to the end of this one
     * @param folio_BaseView $v The view to add
     */
    public function Load( folio_BaseView $v ) {
        $this->html .= "\n" . $v->html();
        $this->js .= "\n" . $v->js();
        $this->metatags = array_unique(
                            array_merge( $this->metatags,
                                          $v->metatags()));
    }

    public function redirect( $_post ) {
        return null;
    }

    public function toString() {
        global $metatags;
        global $CFG;

        // Turn links into the metatags.
        $result = "\n\n<!-- Metatags loaded thru folio_form::LoadMetatags -->";
        foreach( $this->metatags as $link ) {

            // Setup different link refs depending upon the resource.
            if( substr($link,strlen($link) - 3,3) == "css") {
                // CSS
               
                $result = $result . "\n" .
                    '<link rel="stylesheet" type="text/css" href="' .
                    $CFG->wwwroot . $link . '" />';

            } elseif( substr($link,strlen($link) - 2,2) == "js") {
                // Javascript

                $result = $result . "\n" .
                    '<script type="text/javascript" src="' .
                    $CFG->wwwroot . $link . '"></script>';

            } else {
                // RSS

                $result = $result . "\n" .
                    '<link rel="alternate" type="application/rss+xml" title="RSS" ' .
                    'href="' .
                    $CFG->wwwroot . $link . '" />';
            }
        }
        // Add meta-tags
        $metatags .= $result . "\n<!-- End Tags -->\n\n";
//die($metatags);

        // Return the needed html & js
        return "\n\n<!-- Begin folio_BaseView::html() -->\n\n" .
            $this->html .
            "\n\n<!-- Script -->\n\n" .
            "<!-- Begin folio_BaseView::js() -->\n" .
            '<script type="text/javascript">' .
            "\n// <![CDATA[\n\n" .
            $this->js .
            "\n// ]]>\n</script>\n\n" .
            "\n\n<!-- End folio_BaseView::html()/js()-->\n\n";
    }
}

/**
 * This class provides a simple tooltip control thru JS
 * Note that the element must be wrapped in a div with
 *  class="yui-skin-sam" tag.
 */
class folio_Tooltip extends folio_BaseView {

    function __construct( $id, $text ) {
        $this->js = <<< END
myTooltip{$id} = new YAHOO.widget.Tooltip("myTooltip", {
        context:"{$id}",
        preventoverlap:true,
        autodismissdelay:5000,
        text:"{$text}",
        showDelay:100,
        hideDelay:100 } );
END;

        $this->metatags = array(
            'mod/folio/lib/yui_2.4.1/build/container/assets/container.css',
            'mod/folio/lib/yui_2.4.1/build/yahoo-dom-event/yahoo-dom-event.js',
            'mod/folio/lib/yui_2.4.1/build/animation/animation-min.js',
            'mod/folio/lib/yui_2.4.1/build/container/container-min.js'
        );
    }
}

?>
